package com.bigstar.lolkor;

import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.media.AudioManager;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.KeyEvent;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;


public class ChampionList extends Activity implements OnItemSelectedListener, TextWatcher{
	private static final int SORT_ALPH_UP = 0;
	private static final int SORT_ALPH_DOWN = 1;
	private static final int SORT_IP_UP = 2;
	private static final int SORT_IP_DOWN = 3;
	private static final int SORT_RP_UP = 4;
	private static final int SORT_RP_DOWN = 5;
	private int spinnerPostion;
	private int sort_type = SORT_ALPH_UP;
	Button sort_al;
	Button sort_rp;
	Button sort_ip;
	public ArrayList<ChampionTemplate> champlist = new ArrayList<ChampionTemplate>();
	public ArrayList<ChampionTemplate> com_champlist = new ArrayList<ChampionTemplate>();
	ArrayList<String> bookmark = new ArrayList<String>();
	
    @Override
    protected void onResume() {
        ChampionData.load(this);
        ItemData.load(this);
        Rune.load(this);
    	super.onResume();
    }
    
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 
        ChampionData.load(this);
        ItemData.load(this);
        Rune.load(this);
        
		SharedPreferences pref = getSharedPreferences("pref", Activity.MODE_PRIVATE);
		SharedPreferences.Editor editor = pref.edit();
		for(int i = 0; i < 100; i++){
			try{
				bookmark.add(pref.getString("bookmark"+i, "null"));
			}catch(Exception e){
				editor.remove("bookmark"+i);
			}
		}
		editor.commit();
        spinnerPostion = 0;
        champlist.clear();
        com_champlist.clear();
		for(int i = 0; i < ChampionData.champions.size(); i++){
    		champlist.add(ChampionData.champions.get(i));
		}
        setContentView(R.layout.champ_list);
        Spinner spinner = (Spinner) findViewById(R.id.attr_spinner);
        spinner.setOnItemSelectedListener(this);
        
        GridView gridview = (GridView) findViewById(R.id.champ_grid);
        gridview.setAdapter(new ListAdapter(this));

        sort_rp = (Button)this.findViewById(R.id.btn_sort_rp);
        sort_rp.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				champlist.clear();
		        com_champlist.clear();
		        if(spinnerPostion == 1){
					for(int i = 0; i < ChampionData.champions.size(); i++){
						for(int j = 0; j < bookmark.size(); j++){
		    				if(ChampionData.champions.get(i).name_kor.equals(bookmark.get(j))){
				        		champlist.add(ChampionData.champions.get(i));
		    				}
						}
					}
		        }else if(spinnerPostion >= 3){
					String searchTag = getResources().getStringArray(R.array.attr_en)[spinnerPostion-2];
					StringTokenizer st;
					String tag;
					for(int i = 0; i < ChampionData.champions.size(); i++){
				        st = new StringTokenizer(ChampionData.champions.get(i).tags.replaceAll(" ", ""), ",");	
				        while(st.hasMoreTokens()){
				        	tag = st.nextToken();
				        	if(searchTag.equalsIgnoreCase(tag)){
				        		champlist.add(ChampionData.champions.get(i));
				        	}
				        }
					}
				}else if(spinnerPostion == 0){
					for(int i = 0; i < ChampionData.champions.size(); i++){
			    		champlist.add(ChampionData.champions.get(i));
					}
				}
				if(sort_type == SORT_RP_UP){
					int rp_list[] = {975, 585, 260};
					for(int i = 0; i < rp_list.length; i++){
						for(int j = 0; j < champlist.size(); j++){
							if(rp_list[i] == champlist.get(j).rp_point){
					    		com_champlist.add(champlist.get(j));	
							}
						}	
					}
					sort_type = SORT_RP_DOWN;
					sort_rp.setBackgroundResource(R.drawable.sort_down);
					sort_ip.setBackgroundResource(R.drawable.sort_none);
					sort_al.setBackgroundResource(R.drawable.sort_none);
				}else if(sort_type == SORT_RP_DOWN){
					int rp_list[] = {260, 585, 975};
					for(int i = 0; i < rp_list.length; i++){
						for(int j = 0; j < champlist.size(); j++){
							if(rp_list[i] == champlist.get(j).rp_point){
					    		com_champlist.add(champlist.get(j));	
							}
						}	
					}
					sort_type = SORT_RP_UP;
					sort_rp.setBackgroundResource(R.drawable.sort_up);
					sort_ip.setBackgroundResource(R.drawable.sort_none);
					sort_al.setBackgroundResource(R.drawable.sort_none);
				}else{
					int rp_list[] = {260, 585, 975};
					for(int i = 0; i < rp_list.length; i++){
						for(int j = 0; j < champlist.size(); j++){
							if(rp_list[i] == champlist.get(j).rp_point){
					    		com_champlist.add(champlist.get(j));	
							}
						}	
					}
					sort_type = SORT_RP_UP;
					sort_rp.setBackgroundResource(R.drawable.sort_up);
					sort_ip.setBackgroundResource(R.drawable.sort_none);
					sort_al.setBackgroundResource(R.drawable.sort_none);
				}
				
		        GridView gridview = (GridView) findViewById(R.id.champ_grid);
		        gridview.clearDisappearingChildren();
		        gridview.setAdapter(new ListAdapter(ChampionList.this));
				
			}
			
        	
        });
        sort_ip = (Button)this.findViewById(R.id.btn_sort_ip);
        sort_ip.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				champlist.clear();
		        com_champlist.clear();
		        if(spinnerPostion == 1){
					for(int i = 0; i < ChampionData.champions.size(); i++){
						for(int j = 0; j < bookmark.size(); j++){
		    				if(ChampionData.champions.get(i).name_kor.equals(bookmark.get(j))){
				        		champlist.add(ChampionData.champions.get(i));
		    				}
						}
					}
		        }else if(spinnerPostion >= 3){
					String searchTag = getResources().getStringArray(R.array.attr_en)[spinnerPostion-2];
					StringTokenizer st;
					String tag;
					for(int i = 0; i < ChampionData.champions.size(); i++){
				        st = new StringTokenizer(ChampionData.champions.get(i).tags.replaceAll(" ", ""), ",");	
				        while(st.hasMoreTokens()){
				        	tag = st.nextToken();
				        	if(searchTag.equalsIgnoreCase(tag)){
				        		champlist.add(ChampionData.champions.get(i));
				        	}
				        }
					}
		        }else if(spinnerPostion == 0){
					for(int i = 0; i < ChampionData.champions.size(); i++){
			    		champlist.add(ChampionData.champions.get(i));
					}
				}
				if(sort_type == SORT_IP_UP){
					int ip_list[] = {6300, 4800, 3150, 1350, 450};
					for(int i = 0; i < ip_list.length; i++){
						for(int j = 0; j < champlist.size(); j++){
							if(ip_list[i] == champlist.get(j).ip_point){
					    		com_champlist.add(champlist.get(j));	
							}
						}	
					}
					sort_type = SORT_IP_DOWN;
					sort_rp.setBackgroundResource(R.drawable.sort_none);
					sort_ip.setBackgroundResource(R.drawable.sort_down);
					sort_al.setBackgroundResource(R.drawable.sort_none);
				}else if(sort_type == SORT_IP_DOWN){
					int ip_list[] = {450, 1350, 3150, 4800, 6300};
					for(int i = 0; i < ip_list.length; i++){
						for(int j = 0; j < champlist.size(); j++){
							if(ip_list[i] == champlist.get(j).ip_point){
					    		com_champlist.add(champlist.get(j));	
							}
						}	
					}
					sort_type = SORT_IP_UP;
					sort_rp.setBackgroundResource(R.drawable.sort_none);
					sort_ip.setBackgroundResource(R.drawable.sort_up);
					sort_al.setBackgroundResource(R.drawable.sort_none);
				}else{
					int ip_list[] = {450, 1350, 3150, 4800, 6300};
					for(int i = 0; i < ip_list.length; i++){
						for(int j = 0; j < champlist.size(); j++){
							if(ip_list[i] == champlist.get(j).ip_point){
					    		com_champlist.add(champlist.get(j));	
							}
						}	
					}
					sort_type = SORT_IP_UP;
					sort_rp.setBackgroundResource(R.drawable.sort_none);
					sort_ip.setBackgroundResource(R.drawable.sort_up);
					sort_al.setBackgroundResource(R.drawable.sort_none);
				}
		        GridView gridview = (GridView) findViewById(R.id.champ_grid);
		        gridview.clearDisappearingChildren();
		        gridview.setAdapter(new ListAdapter(ChampionList.this));
			}
        	
        });
        sort_al = (Button)this.findViewById(R.id.btn_sort_al);
		sort_al.setBackgroundResource(R.drawable.sort_up);
        sort_al.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				champlist.clear();
		        com_champlist.clear();
		        if(spinnerPostion == 1){
					for(int i = 0; i < ChampionData.champions.size(); i++){
						for(int j = 0; j < bookmark.size(); j++){
		    				if(ChampionData.champions.get(i).name_kor.equals(bookmark.get(j))){
				        		champlist.add(ChampionData.champions.get(i));
		    				}
						}
					}
		        }else if(spinnerPostion >= 3){
					String searchTag = getResources().getStringArray(R.array.attr_en)[spinnerPostion-2];
					StringTokenizer st;
					String tag;
					for(int i = 0; i < ChampionData.champions.size(); i++){
				        st = new StringTokenizer(ChampionData.champions.get(i).tags.replaceAll(" ", ""), ",");	
				        while(st.hasMoreTokens()){
				        	tag = st.nextToken();
				        	if(searchTag.equalsIgnoreCase(tag)){
				        		champlist.add(ChampionData.champions.get(i));
				        	}
				        }
					}
		        }else if(spinnerPostion == 0){
					for(int i = 0; i < ChampionData.champions.size(); i++){
			    		champlist.add(ChampionData.champions.get(i));
					}
				}
				if(sort_type == SORT_ALPH_UP){
					for(int i = champlist.size()-1; i >= 0; i--){
			    		com_champlist.add(champlist.get(i));
					}
					sort_type = SORT_ALPH_DOWN;
					sort_rp.setBackgroundResource(R.drawable.sort_none);
					sort_ip.setBackgroundResource(R.drawable.sort_none);
					sort_al.setBackgroundResource(R.drawable.sort_down);
				}else if(sort_type == SORT_ALPH_DOWN){
					for(int i = 0; i < champlist.size(); i++){
			    		com_champlist.add(champlist.get(i));
					}
					sort_type = SORT_ALPH_UP;
					sort_rp.setBackgroundResource(R.drawable.sort_none);
					sort_ip.setBackgroundResource(R.drawable.sort_none);
					sort_al.setBackgroundResource(R.drawable.sort_up);
				}else{
					for(int i = 0; i < champlist.size(); i++){
			    		com_champlist.add(champlist.get(i));
					}
					sort_type = SORT_ALPH_UP;
					sort_rp.setBackgroundResource(R.drawable.sort_none);
					sort_ip.setBackgroundResource(R.drawable.sort_none);
					sort_al.setBackgroundResource(R.drawable.sort_up);
				}
		        GridView gridview = (GridView) findViewById(R.id.champ_grid);
		        gridview.clearDisappearingChildren();
		        gridview.setAdapter(new ListAdapter(ChampionList.this));
			}
        	
        });
    }
    TextView tv;
    AutoCompleteTextView actv;
	@Override
	public void onItemSelected(AdapterView<?> parent,View v,int position,long id){
		((TextView)parent.getChildAt(0)).setTextColor(Color.WHITE);
		((TextView)parent.getChildAt(0)).setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
		((TextView)parent.getChildAt(0)).setPadding(10, 0, 0, 0);
		if(position == 2){
			String[] names = new String[ChampionData.champions.size() * 2];
			for(int i = 0; i < ChampionData.champions.size(); i++){
				names[i] = ChampionData.champions.get(i).name_en;
			}
			for(int i = 0; i < ChampionData.champions.size(); i++){
				names[ChampionData.champions.size()+i] = ChampionData.champions.get(i).name_kor;
			}
			tv = new TextView(this);
			actv = new AutoCompleteTextView(this);
			actv.setThreshold(1);
			actv.addTextChangedListener(this);
			actv.setAdapter(new ArrayAdapter<String>(this,
					android.R.layout.simple_dropdown_item_1line,
					names));
    		new AlertDialog.Builder(ChampionList.this)
    		.setTitle("Champion의 이름을 입력하세요.Ex)아칼리 or Akali")
    		.setView(actv)
    		.setPositiveButton("찾기", new DialogInterface.OnClickListener() {
    			public void onClick(DialogInterface dialog, int whichButton) {
    				String name = actv.getText().toString();
    				for(int i = 0; i < ChampionData.champions.size(); i++){
    					if(name.equalsIgnoreCase(ChampionData.champions.get(i).name_en)){
    		        		if(getIntent().getExtras() != null){
    		        			BuilderChampion.champ_id = i;
    		        			finish();
    		        			return;
    		        		}
    						SharedPreferences pref = getSharedPreferences("pref", Activity.MODE_PRIVATE);
							boolean menual = pref.getBoolean("menual", true);
    		    			if(menual){
        		    			MenualSelectDialog msdialog = new MenualSelectDialog(ChampionList.this, i);
        		    			msdialog.show();
    		    			}else{
    		    	    		Intent intent = new Intent(ChampionList.this, Champion.class);
    		    				intent.putExtra("champid", ""+i);
    		    				startActivity(intent);
    		    			}
    	               		return;
    					}
    				}
    				for(int i = 0; i < ChampionData.champions.size(); i++){
    					if(name.equalsIgnoreCase(ChampionData.champions.get(i).name_kor)){
    		        		if(getIntent().getExtras() != null){
    		        			BuilderChampion.champ_id = i;
    		        			finish();
    		        			return;
    		        		}
    						SharedPreferences pref = getSharedPreferences("pref", Activity.MODE_PRIVATE);
							boolean menual = pref.getBoolean("menual", true);
    		    			if(menual){
        		    			MenualSelectDialog msdialog = new MenualSelectDialog(ChampionList.this, i);
        		    			msdialog.show();
    		    			}else{
    		    	    		Intent intent = new Intent(ChampionList.this, Champion.class);
    		    				intent.putExtra("champid", ""+i);
    		    				startActivity(intent);
    		    			}
    	               		return;
    					}
    				}
    			}
    		})
    		.create().show();
			return;
		}
		champlist.clear();
        com_champlist.clear();
		spinnerPostion = position;
        if(spinnerPostion == 1){
			for(int i = 0; i < ChampionData.champions.size(); i++){
				for(int j = 0; j < bookmark.size(); j++){
    				if(ChampionData.champions.get(i).name_kor.equals(bookmark.get(j))){
		        		champlist.add(ChampionData.champions.get(i));
    				}
				}
			}
        }else if(spinnerPostion >= 3){
			String searchTag = getResources().getStringArray(R.array.attr_en)[spinnerPostion-2];
			StringTokenizer st;
			String tag;
			for(int i = 0; i < ChampionData.champions.size(); i++){
		        st = new StringTokenizer(ChampionData.champions.get(i).tags.replaceAll(" ", ""), ",");	
		        while(st.hasMoreTokens()){
		        	tag = st.nextToken();
		        	if(searchTag.equalsIgnoreCase(tag)){
		        		champlist.add(ChampionData.champions.get(i));
		        	}
		        }
			}
		}else if(spinnerPostion == 0){
			for(int i = 0; i < ChampionData.champions.size(); i++){
				champlist.add(ChampionData.champions.get(i));
			}
		}
		if(sort_type == SORT_RP_UP){
			int rp_list[] = {260, 585, 975};
			for(int i = 0; i < rp_list.length; i++){
				for(int j = 0; j < champlist.size(); j++){
					if(rp_list[i] == champlist.get(j).rp_point){
			    		com_champlist.add(champlist.get(j));	
					}
				}	
			}
		}else if(sort_type == SORT_RP_DOWN){
			int rp_list[] = {975, 585, 260};
			for(int i = 0; i < rp_list.length; i++){
				for(int j = 0; j < champlist.size(); j++){
					if(rp_list[i] == champlist.get(j).rp_point){
			    		com_champlist.add(champlist.get(j));	
					}
				}	
			}
		}else if(sort_type == SORT_ALPH_UP){
			for(int i = 0; i < champlist.size(); i++){
	    		com_champlist.add(champlist.get(i));
			}
		}else if(sort_type == SORT_ALPH_DOWN){
			for(int i = champlist.size()-1; i >= 0; i--){
	    		com_champlist.add(champlist.get(i));
			}
		}else if(sort_type == SORT_IP_UP){
			int ip_list[] = {450, 1350, 3150, 4800, 6300};
			for(int i = 0; i < ip_list.length; i++){
				for(int j = 0; j < champlist.size(); j++){
					if(ip_list[i] == champlist.get(j).ip_point){
			    		com_champlist.add(champlist.get(j));	
					}
				}	
			}
		}else if(sort_type == SORT_IP_DOWN){
			int ip_list[] = {6300, 4800, 3150, 1350, 450};
			for(int i = 0; i < ip_list.length; i++){
				for(int j = 0; j < champlist.size(); j++){
					if(ip_list[i] == champlist.get(j).ip_point){
			    		com_champlist.add(champlist.get(j));	
					}
				}	
			}
		}
        GridView gridview = (GridView) findViewById(R.id.champ_grid);
        gridview.clearDisappearingChildren();
        gridview.setAdapter(new ListAdapter(this));
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
	}
	
	class ListAdapter extends BaseAdapter {
		private LayoutInflater mInflater;
		 public ListAdapter (Context context) {
			 mInflater = LayoutInflater.from(context);
		 }
		 
		 public int getCount() {
			  return com_champlist.size();
		 }
		 
		 public Object getItem(int position) {
		  return null;
		 }
		 
		 public long getItemId(int position){
		  return 0;
		 }
		 
		 // Create a New ImageView for each item referenced by the Adapter
		 public View getView(int position, View convertView, ViewGroup parent){
			 ViewHolder holder;
			 if (convertView == null) {
				 convertView = mInflater.inflate(R.layout.champ_list_row, null); 
				 holder = new ViewHolder();
				 holder.name = (TextView)convertView.findViewById(R.id.champ_name); 
				 holder.champ_img = (ImageView)convertView.findViewById(R.id.champ_img); 
				 holder.rote_icon = (ImageView)convertView.findViewById(R.id.rote_icon); 
				 holder.bookmark_icon = (ImageView)convertView.findViewById(R.id.bookmark_icon); 
				 LinearLayout layout = (LinearLayout)convertView.findViewById(R.id.point_layout);
				 holder.rp_point = (TextView)layout.findViewById(R.id.tv_rp_point); 
				 holder.ip_point = (TextView)layout.findViewById(R.id.tv_ip_point); 
				 convertView.setTag(holder);
			 }else{
				 holder = (ViewHolder) convertView.getTag();
			 }
			 holder.name.setTag(position);
			 holder.name.setText(com_champlist.get(position).name_kor);  
			 holder.rp_point.setText(""+com_champlist.get(position).rp_point);
			 holder.ip_point.setText(""+com_champlist.get(position).ip_point);
			 holder.rote_icon.setVisibility(View.GONE);
			 holder.bookmark_icon.setVisibility(View.GONE);
			 for(int i = 0; i < 10; i++){
				 if(Lolkor.rota_index[i] == com_champlist.get(position).champ_id){
					 holder.rote_icon.setVisibility(View.VISIBLE);
					 break;
				 }
			 }
			 for(int i = 0; i < bookmark.size(); i++){
				 if(holder.name.getText().toString().equals(bookmark.get(i))){
					 holder.bookmark_icon.setVisibility(View.VISIBLE); 
					 break;
				 }
			 }
			 String path = null;
			 path = "champ_img/"+com_champlist.get(position).name_en+".jpg";
			 try {
				 holder.champ_img.setBackgroundDrawable(Drawable.createFromStream(getAssets().open(path),"src"));
			 } catch (IOException e) {
				 // TODO Auto-generated catch block
				 e.printStackTrace();
			 }
			 
			 convertView.setOnClickListener(new OnClickListener(){

					@Override
					public void onClick(View v) {
			        	ViewHolder holder = (ViewHolder) v.getTag();
			        	int position = (Integer) holder.name.getTag();
						if(getIntent().getExtras() != null){
		        			for(int i = 0; i < ChampionData.champions.size(); i++){
		        				if(ChampionData.champions.get(i).name_en.equals(com_champlist.get(position).name_en)){
		                			BuilderChampion.champ_id = i;
		                			break;
		        				}
		        			}
		        			finish();
		        			return;
		        		}
		        		int champ_id = 0;
		    			for(int i = 0; i < ChampionData.champions.size(); i++){
		    				if(ChampionData.champions.get(i).name_en.equals(com_champlist.get(position).name_en)){
		    					champ_id = i;
		            			break;
		    				}
		    			}
	        			MenualSelectDialog dialog = new MenualSelectDialog(ChampionList.this, champ_id);
	        			dialog.show();	
						
					}
		        });
			 
			 convertView.setOnLongClickListener(new OnLongClickListener() {
			        @Override
			        public boolean onLongClick(View v) {
			        	ViewHolder holder = (ViewHolder) v.getTag();
		        		boolean add = true;
		    			for(int i = 0; i < bookmark.size(); i++){
		    				if(bookmark.get(i).equals(holder.name.getText().toString())){
		    					add = false;
		            			break;
		    				}
		    			}
			    		SharedPreferences pref = getSharedPreferences("pref", Activity.MODE_PRIVATE);
			    		SharedPreferences.Editor editor = pref.edit();
			    		if(add){
			    			for(int i = 0; i < 100; i++){
			    				if(pref.getString("bookmark"+i, "null").equals("null")){
			    					editor.putString("bookmark"+i, holder.name.getText().toString());
			    					bookmark.add(holder.name.getText().toString());
			    					break;
			    				}
			    			}	
			    		}else{
			    			for(int i = 0; i < 100; i++){
			    				if(pref.getString("bookmark"+i, "null").equals(holder.name.getText().toString())){
			    					editor.putString("bookmark"+i, "null");
			    					for(int j = 0; j < bookmark.size(); j++){
			    						if(bookmark.get(j).equals(holder.name.getText().toString())){
				    						bookmark.remove(j);	
			    						}
			    					}
			    				}
			    			}	
			    		}
			    		editor.commit();
			    		ListAdapter.this.notifyDataSetChanged();
			            return true;
			        }
			 });
		  
			 return convertView;
		 }
		 
	}
	 static class ViewHolder {
		 ImageView champ_img;
		 TextView rp_point;
		 TextView ip_point;
		 TextView name;
		 ImageView rote_icon;
		 ImageView bookmark_icon;
		 //TextView tvGoodsLocation;
	 }
	@Override
	public void afterTextChanged(Editable s) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_VOLUME_UP){
			AudioManager audio = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
			int volume = audio.getStreamVolume(AudioManager.STREAM_MUSIC);
			audio.setStreamVolume(AudioManager.STREAM_MUSIC, ++volume, AudioManager.FLAG_SHOW_UI);	
			return true;
		}else if(keyCode == KeyEvent.KEYCODE_VOLUME_DOWN){
			AudioManager audio = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
			int volume = audio.getStreamVolume(AudioManager.STREAM_MUSIC);
			audio.setStreamVolume(AudioManager.STREAM_MUSIC, --volume, AudioManager.FLAG_SHOW_UI);
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		// TODO Auto-generated method stub
		tv.setText(actv.getText());
	}

}
